package com.juc;

/**
 * @author hh
 * @since 2023/6/4
 */
public class DeadLockTest {

    public static void main(String[] args) {
        new Thread(new DeadLock("a", "b"), "T1").start();
        new Thread(new DeadLock("b", "a"), "T2").start();
    }

}

class DeadLock implements Runnable{
    private String lockA;
    private String lockB;

    public DeadLock(String lockA, String lockB) {
        this.lockA = lockA;
        this.lockB = lockB;
    }

    @Override
    public void run() {
        synchronized (lockA) {
            System.out.println(Thread.currentThread().getName() + "持有锁A，准备获取锁B");

            synchronized (lockB) {
                System.out.println(Thread.currentThread().getName() + "持有锁B");
            }
        }
    }

}
